package com.wangsd.common.task;


import com.wangsd.common.utils.DateUtils;
import com.wangsd.web.coupon.entity.Product;
import com.wangsd.web.coupon.entity.TOrder;
import com.wangsd.web.coupon.service.ProductService;
import com.wangsd.web.coupon.service.TOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class MyTask {

    @Autowired
    TOrderService orderService;

    @Autowired
    ProductService productService;

    //每天晚上1点执行分账查询和分账操作
    @Scheduled(fixedRate = 3600000)//一小时执行一次
//    @Scheduled(cron = "0 * */1 * * ?")
    public void BankCardScheduler() {
        log.info("#########未付款完成的，取消订单#########");
        TOrder query = new TOrder();
        query.setOrderStatus(0);
        Date beginTime = DateUtils.getHourBeforeTime(2);
        query.setBeginTime(beginTime);
        Date endTime = DateUtils.getHourBeforeTime(1);
        query.setEndTime(endTime);
        List<TOrder> tOrders = orderService.selectAll(query);
        if (tOrders != null) {
            for (TOrder order:tOrders) {
                String productId = order.getProductId();
                Product product = new Product();
                product.setId(productId);
                product.setType(Integer.valueOf(order.getTradeType()));
                int i = productService.returnProduct(product);
                if(i==1){
                    orderService.removeById(order.getId());
                }
            }
        }

        log.info("#########未付款完成的，取消订单#########");
    }
}
